package com.peng.leetcode.other;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * ReverseKGroup
 *
 * @author: lupeng6
 * @create: 2020/11/15 14:16
 */
public class ReverseKGroup {


    public static void main(String[] args) {
//        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
//        head = new Solution().reverseKGroup(head, 2);
//        System.out.println(head);

        System.out.println('a' ^ 'a');

    }

    static class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            Deque<ListNode> stack = new ArrayDeque<ListNode>();
            ListNode dummy = new ListNode(0);
            ListNode p = dummy;
            while (true) {
                int count = 0;
                ListNode tmp = head;
                while (tmp != null && count < k) {
                    stack.add(tmp);
                    tmp = tmp.next;
                    count++;
                }
                if (count != k) {
                    p.next = head;
                    break;
                }
                while (!stack.isEmpty()){
                    p.next = stack.pollLast();
                    p = p.next;
                }
                p.next = tmp;
                head = tmp;
            }
            return dummy.next;
        }
    }
}
